{% extends "base.html" %}

{% block title %}课程管理 - 学生成绩管理系统{% endblock %}

{% block extra_css %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/common.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/admin/course.css') }}">
{% endblock %}

{% block content %}
<div class="course-wrapper">
    <!-- 页面标题和操作按钮 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="page-title">
            <i class="fas fa-book text-primary"></i>
            课程管理
        </h2>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addCourseModal">
            <i class="fas fa-plus"></i> 添加课程
        </button>
    </div>

    <!-- 搜索和筛选区域 -->
    <div class="card mb-4">
        <div class="card-body">
            <div class="row">
                <div class="col-md-2">
                    <div class="input-group">
                        <span class="input-group-text"><i class="fas fa-search"></i></span>
                        <input type="text" class="form-control" id="searchInput" placeholder="搜索课程名称或代码...">
                    </div>
                </div>
                <div class="col-md-2">
                    <select class="form-select" id="creditsFilter">
                        <option value="">所有学分</option>
                        <option value="1.0">1.0</option>
                                    <option value="1.5">1.5</option>
                                    <option value="2.0">2.0</option>
                                    <option value="2.5">2.5</option>
                                    <option value="3.0">3.0</option>
                                    <option value="3.5">3.5</option>
                                    <option value="4.0">4.0</option>
                                    <option value="4.5">4.5</option>
                                    <option value="5.0">5.0</option>
                    </select>
                </div>
                <div class="col-md-1">
                    <button type="button" class="btn btn-outline-secondary w-100" onclick="resetFilters()">
                        <i class="fas fa-redo"></i> 重置
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- 课程列表 -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">
                <i class="fas fa-list"></i> 课程列表
                <span class="badge bg-primary ms-2" id="listCount">0</span>
            </h5>
        </div>
        <div class="card-body">
            <!-- 加载状态 -->
            <div id="loadingSpinner" class="text-center py-4">
                <div class="spinner-border text-primary" role="status">
                    <span class="visually-hidden">加载中...</span>
                </div>
                <p class="mt-2 text-muted">正在加载课程数据...</p>
            </div>

            <!-- 课程表格 -->
            <div class="table-responsive" id="courseTableContainer" style="display: none;">
                <table class="table table-hover" id="courseTable">
                    <thead class="table-light">
                        <tr>
                            <th>课程ID</th>
                            <th>课程名称</th>
                            <th>课程代码</th>
                            <th>学分</th>
                            <th>课程描述</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="courseTableBody">
                        <!-- 课程数据将通过JavaScript动态加载 -->
                    </tbody>
                </table>
            </div>

            <!-- 空状态 -->
            <div id="emptyState" class="text-center py-5" style="display: none;">
                <i class="fas fa-book fa-3x text-muted mb-3"></i>
                <h5 class="text-muted">暂无课程数据</h5>
                <p class="text-muted">点击上方"添加课程"按钮开始添加课程</p>
            </div>
        </div>
    </div>

    <!-- 分页 -->
    <div aria-label="课程分页" class="mt-4">
        <ul class="pagination justify-content-center" id="pagination">
            <!-- 分页按钮 -->
        </ul>
    </div>
</div>

<!-- 添加课程模态框 -->
<div class="modal fade" id="addCourseModal" tabindex="-1" aria-labelledby="addCourseModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addCourseModalLabel">
                    <i class="fas fa-plus text-primary"></i> 添加课程
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="addCourseForm">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="addCourseName" class="form-label">课程名称 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="addCourseName" name="name" required>
                                <div class="invalid-feedback">请输入课程名称</div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="addCourseCode" class="form-label">课程代码 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="addCourseCode" name="code" required>
                                <div class="invalid-feedback">请输入课程代码</div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="addCourseCredits" class="form-label">学分 <span class="text-danger">*</span></label>
                                <select class="form-select" id="addCourseCredits" name="credits" required>
                                    <option value="">请选择学分</option>
                                    <option value="1.0">1.0</option>
                                    <option value="1.5">1.5</option>
                                    <option value="2.0">2.0</option>
                                    <option value="2.5">2.5</option>
                                    <option value="3.0">3.0</option>
                                    <option value="3.5">3.5</option>
                                    <option value="4.0">4.0</option>
                                    <option value="4.5">4.5</option>
                                    <option value="5.0">5.0</option>
                                </select>
                                <div class="invalid-feedback">请选择学分</div>
                            </div>
                        </div>

                    </div>
                    <div class="mb-3">
                        <label for="addCourseDescription" class="form-label">课程描述</label>
                        <textarea class="form-control" id="addCourseDescription" name="description" rows="3" placeholder="请输入课程描述..."></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> 保存课程
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑课程模态框 -->
<div class="modal fade" id="editCourseModal" tabindex="-1" aria-labelledby="editCourseModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editCourseModalLabel">
                    <i class="fas fa-edit text-warning"></i> 编辑课程
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="editCourseForm">
                <div class="modal-body">
                    <input type="hidden" id="editCourseId" name="id">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editCourseName" class="form-label">课程名称 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="editCourseName" name="name" required>
                                <div class="invalid-feedback">请输入课程名称</div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editCourseCode" class="form-label">课程代码 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="editCourseCode" name="code" required>
                                <div class="invalid-feedback">请输入课程代码</div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editCourseCredits" class="form-label">学分 <span class="text-danger">*</span></label>
                                <select class="form-select" id="editCourseCredits" name="credits" required>
                                    <option value="">请选择学分</option>
                                    <option value="1.0">1.0</option>
                                    <option value="1.5">1.5</option>
                                    <option value="2.0">2.0</option>
                                    <option value="2.5">2.5</option>
                                    <option value="3.0">3.0</option>
                                    <option value="3.5">3.5</option>
                                    <option value="4.0">4.0</option>
                                    <option value="4.5">4.5</option>
                                    <option value="5.0">5.0</option>
                                </select>
                                <div class="invalid-feedback">请选择学分</div>
                            </div>
                        </div>

                    </div>
                    <div class="mb-3">
                        <label for="editCourseDescription" class="form-label">课程描述</label>
                        <textarea class="form-control" id="editCourseDescription" name="description" rows="3" placeholder="请输入课程描述..."></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-warning">
                        <i class="fas fa-save"></i> 更新课程
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteCourseModal" tabindex="-1" aria-labelledby="deleteCourseModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteCourseModalLabel">
                    <i class="fas fa-exclamation-triangle text-danger"></i> 确认删除
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>您确定要删除课程 <strong id="deleteCourseInfo"></strong> 吗？</p>
                <div class="alert alert-warning">
                    <i class="fas fa-exclamation-triangle"></i>
                    <strong>警告：</strong>删除课程将同时删除该课程的所有成绩记录，此操作不可撤销！
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" id="confirmDeleteBtn">
                    <i class="fas fa-trash"></i> 确认删除
                </button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="{{ url_for('static', filename='js/admin/course.js') }}"></script>
{% endblock %}